#include "bsp_led.h"
#include "bsp_beep.h"
#include "bsp_key.h"
#include "bsp_tube.h"
#include "RTC.h"
#include "Control.h"

void Control(void)
{
		u8 value = 0;
		u8 hour = 0,min = 0;
		u8 A_hour = 0,A_min = 0;
	
    hour = HOUR;
    min = MIN;
    Tube_Display(hour,min,1,4);
    value = KEY_Scan(0);
    if(value == 1)
    {
        do
        {
            Tube_Display(hour,min,1,2);
            value = KEY_Scan(0);
            if(value == 2)
            {
                do
                {
                    Tube_Display(hour,min,3,2);
                    value = KEY_Scan(0);
                    if(value == 3)
                    {
                        min++;
											  if(min>=60)min=0;
                    }
                    else if(value == 4)
                    {
                        min--;
											  if(min<0)min=59;
                    }
                    else if(value == 2)
                    {
                        A_hour = HOUR;
                        A_min  = MIN;
                        do
                        {
                            Tube_Display(A_hour,A_min,1,2);
                            value = KEY_Scan(0);
                            if(value == 3)
                            {
                                A_hour++;
															  if(A_hour>=24)A_hour=0;
                            }
                            else if(value == 4)
                            {
                                A_hour--;
															  if(A_hour<0)A_hour=23;
                            }
                            else if(value == 2)
                            {
                                do
                                {
                                    Tube_Display(A_hour,A_min,3,2);
                                    value = KEY_Scan(0);
                                    if(value == 3)
                                    {
                                        A_min++;
																			  if(A_min>=60)A_min=0;
                                    }
                                    else if(value == 4)
                                    {
                                        A_min--;
																			  if(A_min<60)A_min=59;
                                    }
                                    else if(value == 2)break;
                                } while(!(value == 1));
                                RTC_SetAlarm(A_hour,A_min);
                                break;
                            }
                        } while(!(value == 1));
                    }
                } while(!(value == 1));
                break;
            }
            else if(value == 3)
            {
                hour++;
							  if(hour >= 24)hour = 0;
            }
            else if(value == 4)
            {
                hour--;
							  if(hour < 0)hour = 23;
            }
        } while(!(value == 1));
				Time[0] = INIYEAR;
				Time[1] = INIMONTH;
				Time[2] = INIDAY;
        Time[3] = hour;
        Time[4] = min;
				Time[5] = INISEC;
				Time[6] = INISSEC;
				RTC_SetTime();
    }
}